package com.briup.mt.web.controller;


import com.briup.mt.bean.Application;
import com.briup.mt.bean.BaseUser;
import com.briup.mt.service.IBaseUserService;
import com.briup.mt.utils.ExcelUtils;
import com.briup.mt.utils.Message;
import com.briup.mt.utils.MessageUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Validated
@RequestMapping("/baseUser")
public class BaseUserController {

    @Autowired
    private IBaseUserService baseUserService;

    @GetMapping("query")
    public Message query(String username,String gender,String address){
        List<BaseUser> list = baseUserService.query(username,gender,address);
        return MessageUtil.success(list);
    }

    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(BaseUser baseUser){
        baseUserService.saveOrUpdate(baseUser);
        return MessageUtil.success(baseUser);

    }

    @DeleteMapping("deleteById")
    public Message deleteById(Long id){
        baseUserService.deleteById(id);
        return MessageUtil.success("操作成功");
    }

    @ApiOperation(value = "导入到Excel中", notes = "注意！测试的时候请将地址粘贴到浏览器地址栏测试", produces = "application/octet-stream")
    @GetMapping("download")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query"),
    })
    public void download(HttpServletResponse response, String username, String gender, String address) throws Exception {
        List<BaseUser> list = baseUserService.query(username, gender,address);
        // 2. 将excel导出
        // 2.1 excel头部数据
        String[] headList = new String[] { "用户名", "密码","角色", "姓名", "性别", "联系电话", "email", "地址"};
        // 2.2 属性
        String[] fieldList = new String[] { "username", "password", "rolename", "realname", "gender", "telephone", "email","address" };
        // 2.3 数据列，需要转化
        List<Map<String, Object>> dataList = new ArrayList<>();

        for (BaseUser app : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("username", app.getUsername());
            map.put("password", app.getPassword());
            map.put("rolename", app.getRolename());
            map.put("realname", app.getRealname());
            map.put("gender", app.getGender());
            map.put("telephone", app.getTelephone());
            map.put("email", app.getEmail());
            map.put("address", app.getAddress()
            );

            dataList.add(map);
        }
        // 2.4 导出文件名
        String excelName = "user";
        // 3. 调用工具方法导出Excel
        ExcelUtils.createExcel(response, excelName, headList, fieldList, dataList);

    }

}
